<template>
  <div class="drawer_box" style="display: flex;">
    <!-- <div class="drawer_text">{{ CriminalInfoSelectText }} </div> -->


    <div style="width: 17%; margin-right: 10px">
        <el-tree
          ref="tree"
          :data="optionStree"
          :props="defaultProps"
          @node-click="handleNodeClick"
          :highlight-current="true"
          node-key="id"
          :check-strictly="true"
          :expand-on-click-node="false"
          :default-expand-all="true"
        ></el-tree>
      </div>
   <div style="width: 83%;">
         <div
      class="flex_bw_center serch_box"
      style="transform: translateX(-11px); margin-bottom: 4px"
    >
      <div>
        <!-- <el-input
        v-model="searchName"
        placeholder="请输入查询姓名"
        class="filter-item"
        style="margin-left: 10px; width: 200px"
      /> -->
        <el-button
          title="新增"
          class="filter-item"
          style="margin-left: 10px"
          type="primary"
          :icon="Plus"
          @click="getDialog"
         
        >
        </el-button>
      </div>
         </div>
     
         <div class="isTable">
      <el-table
        :data="CriminalInfoSelectList"
        border
        fit
        highlight-current-row
        height="682"
        stripe
      >
        <el-table-column type="index" align="center" width="60" label="序号">
        </el-table-column>
        <el-table-column label="名称" align="center" width="240">
          <template #default="scope">
            <span>{{ scope.row.value }}</span>
          </template>
        </el-table-column>

        <el-table-column label="描述" align="center">
          <template #default="scope">
            <span>{{ scope.row.content }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="160">
          <template #default="scope">
            <el-button size="small" @click="save(scope.row)" type="primary">
              编辑
            </el-button>
            <el-popconfirm
              title="确认删除嘛"
              @confirm.stop="handleDelete(scope.row)"
            >
              <template #reference>
                <el-button size="small" type="danger"> 删除 </el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
         </div>
   </div> 


  </div>
  <div>
    <el-dialog title="" v-model="dialogFormVisibleCv" width="1152">
      <div
        class=""
        style="margin-top: 10px; height: 537px; box-sizing: border-box"
      >
        <div class="flex_start_start form_item">
          <div class="titles flex_center_center">名称：</div>
          <el-input
            v-model="formData.value"
            style="width: 340px"
            placeholder=""
          />
        </div>
        <div class="flex_start_start form_item">
          <div class="titles flex_center_center">描述：</div>
          <el-input
            type="textarea"
            :rows="4"
            v-model="formData.content"
            style="width: 340px"
            placeholder=""
          />
        </div>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="SaveFun"> 保存 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import http from "@/utils/api";
import {
  Search,
  Share,
  Upload,
  TakeawayBox,
  Refresh,
  Collection,
  Plus,
  Loading,
  FolderDelete,
  Back,
} from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import {
  ref,
  watch,
  onMounted,
  onBeforeUnmount,
  reactive,
  onBeforeMount,
  nextTick,
  onUnmounted,
} from "vue";
import { defineProps, defineEmits } from "vue";
const props = defineProps({
  sonData: {
    type: String,
    default: "",
  },
  orgIds: {
    type: Object,
    default: {},
  },
});
let defaultProps = ref({
  children: "childrens",
  label: "value",
  value:'id'
});


let dialogFormVisibleCv = ref(false);
let optionStree = ref([])
let CriminalInfoSelectList = ref([
]);
let formData = ref({});
let stateObj = ref({});
onBeforeMount(() => {
  stateObj.value = JSON.parse(sessionStorage.getItem("stateObj"));
  getFormattedDateTime();
  getCriminalInfoSelect();
});
let dateSfm = ref("");
let hour = ref("");
let daydata = ref("");
let faObj = ref({});
let types = ref();
let pids = ref(0)
let pCode = ref("");

let tree = ref(null)
const getCriminalInfoSelect = async () => {
  let res = await http.cmd.sysCmdTree({
    code: "jqqksb,tfsj,zyhy,qtsx",
    pId: "725,745,748,752",
    type: 72,
    value: "",
  });
  if (res && res.code == 0) {
    if(res.data.length){
      optionStree.value = res.data;
      pids.value = res.data[0].id;
      types.value = res.data[0].type;
      faObj.value = res.data[0]
      setTimeout(() => {
        if( tree.value){

          tree.value.setCurrentKey(optionStree.value[0].id);
        }
        });
        
        getsysCmdTree()
    }else{
      optionStree.value = []
    }
  }
};

const getsysCmdTree = async()=>{
  let res = await http.cmd.sysCmdTree({
    code: "",
    pId: pids.value,
    type: 72,
    value: "",
  });
  if(res && res.data){
    CriminalInfoSelectList.value = res.data || []
  }
}

const handleDelete = async (row) => {
  let res = await http.cmd.SysCmdDel({ id: row.id });
  if (res && res.code == 0) {
    ElMessage({
      message: "删除成功",
      type: "success",
      plain: true,
    });
    getCriminalInfoSelect();
  }
};

const handleNodeClick = (value) => {
   
      if(value){
        pids.value = value.id;
		pCode.value = value.code
        getsysCmdTree()
      }
      
}

const getDialog = () => {
  let obj = {
    pId:  pids.value||0,
    type: types.value,
    level: 3,
    sort: 1,
    code: pCode.value,
  };
  formData.value = Object.assign({}, obj);
  dialogFormVisibleCv.value = true;
};

// 获取年月日 时
const getFormattedDateTime = () => {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, "0");
  const day = String(now.getDate()).padStart(2, "0");
  const hours = String(now.getHours()).padStart(2, "0");
  const minutes = String(now.getMinutes()).padStart(2, "0");
  const seconds = String(now.getSeconds()).padStart(2, "0");
  dateSfm.value = `${hours}:${minutes}:${seconds}`;
  hour.value = String(now.getHours()).padStart(2, "0");
  daydata.value = `${year}-${month}-${day}`;

  return `${year}-${month}-${day} ${hour.value}`;
};

const SaveFun = async () => {
  if (formData.value?.id) {
    let res = await http.base.sysCmdUpdate(formData.value);
    if (res && res.code == 0) {
      ElMessage({
        message: "保存成功",
        type: "success",
        plain: true,
      });
      getsysCmdTree();
      dialogFormVisibleCv.value = false;
    }
  } else {
	  console.log(formData.value)
    let res = await http.base.SysCmdAdd(formData.value);
    if (res && res.code == 0) {
      ElMessage({
        message: "保存成功",
        type: "success",
        plain: true,
      });
      getsysCmdTree();
      dialogFormVisibleCv.value = false;
    }
  }
};
const save = (row) => {
  let obj = {
    id: row.id,
    value: row.value,
    content: row.content,
	
  };
  formData.value = Object.assign({}, obj);
  dialogFormVisibleCv.value = true;
};
</script>

<style scoped lang="scss">

::v-deep .el-tree-node__content:hover{
  background-color: #ecf5ff54;
}
::v-deep
  .el-tree--highlight-current
  .el-tree-node.is-current
  > .el-tree-node__content {
  background-color: #ecf5ff54;
}
::v-deep .el-tree {
  background: none;
  color: white;
}

::v-deep .el-input__inner{
  color: white;
}
::v-deep .el-textarea__inner {
  background: #ffffff0d;
  box-shadow: none;
  color: white;
}
::v-deep .el-input__wrapper {
    background-color: rgb(255 255 255 / 17%);
    box-shadow: (0 0 0 1px #dcdfe600) inset;
  }
.form_item {
  width: 100%;
  height: 40px;
  margin-bottom: 10px;
  color: white;
}
.titles {
  min-width: 60px;
  height: 100%;
}
.nub_box {
  padding-left: 20px;
  color: white;
  font-size: 14px;
}

::v-deep .example-pagination-block {
  margin-top: 10px;
  margin-left: 230px;
  .el-pagination.is-background .btn-prev {
    background-color: #34517d3b;
    color: #ffffff;
  }
  .el-pagination.is-background .el-pager li {
    background-color: #34517d3b;
    color: #ffffff;
  }
  .el-pagination.is-background .btn-next {
    background-color: #34517d3b;
    color: #ffffff;
  }
  .el-pagination__total {
    color: #ffffff;
  }
  .el-pager li.is-active {
    background-color: #0768f9d3 !important;
  }
}

::v-deep .serch_box {
  .el-input__wrapper {
    background-color: #ffffffc7;
    box-shadow: (0 0 0 1px #dcdfe600) inset;
  }
  .el-input__inner {
  }
}

.isTable {
  // border-top: 1px solid rgb(23, 88, 158) ;
  // border-left: 1px solid rgb(23, 88, 158) ;
}
::v-deep .isTable .el-table {
  thead tr .el-table__cell {
    border-bottom: none !important;
  }

  .el-table__cell {
    padding: 2px 0 !important;
    background: rgb(8, 38, 78) !important;
  }
  .el-table th.el-table__cell.is-leaf {
    background: rgb(8, 38, 78) !important;
  }
  .el-table .cell {
    font-size: 12px !important;
  }
  .el-table .el-table__cell {
    padding: 2px 4px;
  }
  .el-table td.el-table__cell div {
    box-sizing: border-box;
    color: #ffffff !important;
    height: auto;
    line-height: 38px;
  }
  --el-table-border-color: rgb(23, 88, 158);
  --el-table-border: 1px solid rgb(23, 88, 158);
  .cell {
    height: 35px;
    text-align: center;
    // border: 1px solid #ccc;
    color: white;
    font-size: 12px;
    line-height: 35px;
  }
  --el-table-bg-color: #fcfcfc00;
  .el-scrollbar {
    border: 1px solid rgb(23, 88, 158);
    border-right: none;
    border-left: none;
  }
}
.drawer_box {
  width: 100%;
  height: 790px;
  position: relative;
  box-sizing: border-box;
  padding: 0px 10px;
  .drawer_text {
    position: absolute;
    top: -28px;
    left: 2px;
    font-size: 18px;
  }
}
</style>
